/**
 * FileName: PatientController
 * Author:   P
 * Date:     2021/2/24 13:56
 * Description:
 */
package com.hospital.nurse.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.nurse.mapper.PatientMapper;
import com.hospital.nurse.pojo.Patient;
import com.hospital.nurse.service.PatientService;
import com.hospital.nurse.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@CrossOrigin
public class PatientController {

    @Autowired
    private PatientService patientService;

 /*   @RequestMapping("/allpatient/{page}/{size}")
    public IPage<Patient>  allPatient(@PathVariable int page,@PathVariable int size){
        IPage<Patient> patientPage = new Page<>(page,size);
        patientPage = patientService.page(patientPage);
        //List<Patient> list = patientService.list(null);
        return  patientPage;
    }*/
 @RequestMapping("/allpatient/{page}")
 public Map<String, Object>  allPatient(@PathVariable int page, HttpServletRequest request){
     String token = request.getHeader("token");
     System.out.println("token = "+token);
     DecodedJWT verify = JWTUtils.verify(token);

       System.out.println("用户id = "+verify.getClaims().get("did").asString());
       System.out.println("用户id = "+verify.getClaims().get("username").asString());

     Map<String, Object> map = new HashMap<>();
     System.out.println(patientService.findAllPatient(page).toString());
     map.put("patients",patientService.findAllPatient(page));
     return  map;
 }

        //删除
    @RequestMapping("/deletePatient/{id}")
    public String deletePatient(@PathVariable int id){

        System.out.println(" id = "+id);
        boolean flag = patientService.removeById(id);
        if (flag){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }


    //查询单个病人
    @RequestMapping("/queryPatient/{id}")
    public Patient getPatient(@PathVariable int id){
        return patientService.getById(id);
    }


    //更换科室
    @RequestMapping("/editPatient")
    public String editPatient(@RequestBody Patient patient){
       // System.out.println(patient.toString());
      if (patientService.updateById(patient)){
          return "更新成功";
      }
      return "更新失败";
    }

    //添加病人
    @RequestMapping("/addPatient")
    public String addPatient(@RequestBody Patient patient){
        patientService.save(patient);
        return "添加成功";
    }

}
